from torch.utils.tensorboard import SummaryWriter
import random
import time

# 创建 SummaryWriter 对象，指定日志保存目录
writer = SummaryWriter('logs')

# 模拟训练过程，记录 100 个步骤的标量数据
for step in range(100):
    # 生成随机的损失值和准确率作为示例数据
    loss = random.randint(0, 10)

    # 记录损失值到 TensorBoard
    writer.add_scalar('Loss', loss, step)

    # 打印当前步骤和记录的数据
    print(f"Step {step}: Loss = {loss:.4f}")

    # 模拟训练过程中的延迟
    time.sleep(0.001)

# 关闭 SummaryWriter
writer.close()
